<?php

/* * ************************************************************************************************
  |
  | AudioShare - Audio Sharing Script
  | http://www.audiosharescript.com
  | webmaster@audiosharescript.com
  | Version: 2.0
  |
  |**************************************************************************************************
  |
  | By using this software you agree that you have read and acknowledged our End-User License
  | Agreement available at http://www.audiosharescript.com/eula.html and to be bound by it.
  |
  | Copyright (c) 2009 AudioShareScript.com. All rights reserved.
  |************************************************************************************************* */

include("../include/config.php");
include_once("../include/functions/import.php");
include_once("../include/functions/admin.php");
verify_login_admin();

function insert_get_all_members() {
    global $config, $conn;
    $query = "select USERID,username from members order by username asc";
    $results = $conn->execute($query);
    $returnthis = $results->getrows();
    return $returnthis;
}

function insert_get_all_cats() {
    global $config, $conn;
    $query = "select CATID,name from categories_audios order by name asc";
    $results = $conn->execute($query);
    $returnthis = $results->getrows();
    return $returnthis;
}

$AUDIOID = intval($_REQUEST['AUDIOID']);

if ($_POST['submitform'] == "1") {
    $_POST["date_added"] = $_POST["year"] . "-" . $_POST["month"] . "-" . $_POST["day"];

    ## move file from one year to another year...
    $prevDate = $_POST["AudioDate"];
    $prevYear = date("Y", strtotime($prevDate));
    
    $prevCategory=$_POST["AudioCategory"];
    $newCategory=$_POST["categories"];
    
    if ($prevYear != $_POST["year"] || $prevCategory!=$newCategory) {
        $catName=fetch_category_name_from_id($newCategory);
        $prevcatName=fetch_category_name_from_id($prevCategory);
        
        if(!is_dir($config['basedir'] . "/adata/".$catName))
             mkdir($config['basedir'] . "/adata/".$catName);
            
            if(!is_dir($config[basedir] . "/adata/".$catName."/".$_POST["year"]))
                mkdir($config['basedir'] . "/adata/".$catName."/".$_POST["year"]);
            
        /*if (!is_dir($config[basedir] . "adata/" . $_POST["year"]))
            mkdir($config['basedir'] . "adata/'" . $_POST["year"]);*/
        $ext = $_POST["AudioExt"];
        $original = $config['basedir'] . "/adata/" .$catName."/". $_POST["year"] . "/" . $AUDIOID . $ext;
        $uploaded = $config['basedir'] . "/adata/" .$prevcatName."/". $prevYear . "/" . $AUDIOID . $ext;
        copy($uploaded, $original);
        unlink($uploaded);

        ## also change the audio path in the xml files...
        $query = "select PLAYLISTID from playlist_audios where AUDIOID='" . $AUDIOID . "'";
        $execute = $conn->execute($query);
        $audioIds = $execute->getrows();

        if (!empty($audioIds)) {
            $playlistids = array();
            foreach ($audioIds as $key => $val) {
                if (!in_array($val["PLAYLISTID"], $playlistids))
                    $playlistids[] = $val["PLAYLISTID"];
            }
        }
        //create_playlist_xml_file
    }
    ## file moved...

    if ($AUDIOID > 0) {
        $arr = array("USERID", "title", "tags", "artist", "country", "categories", "public", "viewcount", "downloadcount", "featured", "ratingcount", "rating", "allowcomments", "allowratings", "allowembeds", "allowdownloads", "active", "mature", "date_added");
        foreach ($arr as $value) {
            $sql = "update audios set $value='" . mysql_real_escape_string($_POST[$value]) . "' where AUDIOID='" . mysql_real_escape_string($AUDIOID) . "'";
            $conn->execute($sql);
        }

        $sql = "update audios set description='" . mysql_real_escape_string($_POST[vdescription]) . "' where AUDIOID='" . mysql_real_escape_string($AUDIOID) . "'";
        $conn->execute($sql);
        if (!empty($playlistids))
            create_playlist_xml($playlistids, 1);
        $message = "Audio Successfully Edited.";
        Stemplate::assign('message', $message);
    }
}

$day = date("d");
$month = date("m");
$year = date("Y");
if ($AUDIOID > 0) {
    $query = $conn->execute("select * from audios where AUDIOID='" . mysql_real_escape_string($AUDIOID) . "' limit 1");
    $audio = $query->getrows();


    $day = date("d", strtotime($audio[0]["date_added"]));
    $month = date("m", strtotime($audio[0]["date_added"]));
    $year = date("Y", strtotime($audio[0]["date_added"]));
    $countriesList = listcountries($audio[0]["country"]);

    STemplate::assign('countriesList', $countriesList);
    Stemplate::assign('audio', $audio[0]);
}

$dayList = listdays($day);
$monthList = listmonths($month);
$yearList = listyears($year);

$mainmenu = "6";
$submenu = "1";

Stemplate::assign('dayList', $dayList);
Stemplate::assign('monthList', $monthList);
Stemplate::assign('yearList', $yearList);


Stemplate::assign('mainmenu', $mainmenu);
Stemplate::assign('submenu', $submenu);
STemplate::display("administrator/global_header.tpl");
STemplate::display("administrator/audios_edit.tpl");
STemplate::display("administrator/global_footer.tpl");
?>